﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.Common;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.Utils.Menu;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using ERP_RORZE_SYSTEM;
using ERP_RORZE_TOOL.Common;
using ERP_RORZE_TOOL.Tool.BCThoigianlamviecthang;

namespace ERP_RORZE_TOOL.Tool.NGNoibo
{
    public partial class ucNGNoibo : DevExpress.XtraEditors.XtraUserControl
    {
        DXPopupMenu menu;
        private int reportId = -1;
        public DateTime dateInput, from, to;

        public string month;
        public bool isCustomer, isMistake;

        DataTable dt = new DataTable();

        public ucNGNoibo()
        {
            InitializeComponent();
            InitPopupMenu();
            BindDataReportList();
            BindDataItemList();
            this.dropbtExport.DropDownControl = menu;
            lblFormName.Text = ReportConstants.FormName;
        }

        private void cmbReportList_SelectionChangeCommitted(object sender, EventArgs e)
        {
            ReportConstants.Item item = (ReportConstants.Item)cmbReportList.SelectedItem;
            cmbData.SelectedItem = cmbData.Items[0];
            if (item.Value != -1)
            {
                BindDataToGridview(item.Value);
                SwitchReport(item.Value);
                reportId = item.Value;
            }
        }

        private void cmbData_SelectionChangeCommitted(object sender, EventArgs e)
        {
            ReportConstants.Item item = (ReportConstants.Item)cmbData.SelectedItem;
            cmbReportList.SelectedItem = cmbReportList.Items[0];
            if (item.Value != -1)
            {

                gridView1.Columns.Clear();
                panelBottom.Visible = true;
                this.reportId = item.Value;
                BindDataToGridview(item.Value);
            }
        }

        private void gridView1_CustomDrawEmptyForeground(object sender, DevExpress.XtraGrid.Views.Base.CustomDrawEventArgs e)
        {
            if (gridView1.RowCount > 0)
                return;

            StringFormat drawFormat = new StringFormat();

            drawFormat.Alignment = drawFormat.LineAlignment = StringAlignment.Center;

            e.Graphics.DrawString("No items exist in this view", e.Appearance.Font, SystemBrushes.ControlDark, new RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height), drawFormat);

        }

        private void BindDataToGridview(int reportId)
        {
            gridView1.Columns.Clear();
            switch (reportId)
            {
                case ReportConstants.ReportId.QChartTong_Qty_Crosstab:
                    gridView1.OptionsBehavior.Editable = false;
                    dt = ReportDAO.ExcuteProcedure(ReportConstants.StoredProcedure.QChartTong_Qty_Crosstab);
                    gridView1.ViewCaption = ReportConstants.ReportName.QChartTong_Qty_Crosstab;
                    break;
                case ReportConstants.ReportId.QChartTong_Amount_Crosstab:
                    gridView1.OptionsBehavior.Editable = false;
                    dt = ReportDAO.ExcuteProcedure(ReportConstants.StoredProcedure.QChartTong_Amount_Crosstab);
                    gridView1.ViewCaption = ReportConstants.ReportName.QChartTong_Amount_Crosstab;
                    break;
                case ReportConstants.ReportId.QChartNguyenNhan_Qty_Crosstab:
                    gridView1.OptionsBehavior.Editable = false;
                    dt = ReportDAO.ExcuteProcedure(ReportConstants.StoredProcedure.QChartNguyenNhan_Qty_Crosstab);
                    gridView1.ViewCaption = ReportConstants.ReportName.QChartNguyenNhan_Qty_Crosstab;
                    break;
                case ReportConstants.ReportId.QChartNguyenNhan_Amount_Crosstab:
                    gridView1.OptionsBehavior.Editable = false;
                    dt = ReportDAO.ExcuteProcedure(ReportConstants.StoredProcedure.QChartNguyenNhan_Amount_Crosstab);
                    gridView1.ViewCaption = ReportConstants.ReportName.QChartNguyenNhan_Amount_Crosstab;
                    break;
                case ReportConstants.ReportId.SPND:
                    gridView1.OptionsBehavior.Editable = false;
                    dt = ReportDAO.ExcuteProcedure(ReportConstants.StoredProcedure.SPND);
                    gridView1.ViewCaption = ReportConstants.ReportName.SPND;
                    break;
                case ReportConstants.ReportId.QSP_ND_Qty_Crosstab:
                    gridView1.OptionsBehavior.Editable = false;
                    dt = ReportDAO.ExcuteProcedure(ReportConstants.StoredProcedure.QSP_ND_Qty_Crosstab);
                    gridView1.ViewCaption = ReportConstants.ReportName.QSP_ND_Qty_Crosstab;
                    break;
                case ReportConstants.ReportId.QSP_ND_Amount_Crosstab:
                    gridView1.OptionsBehavior.Editable = false;
                    dt = ReportDAO.ExcuteProcedure(ReportConstants.StoredProcedure.QSP_ND_Amount_Crosstab);
                    gridView1.ViewCaption = ReportConstants.ReportName.QSP_ND_Amount_Crosstab;
                    break;
                case ReportConstants.ReportId.QSP_NguyenNhan_Qty_Crosstab:
                    gridView1.OptionsBehavior.Editable = false;
                    dt = ReportDAO.ExcuteProcedure(ReportConstants.StoredProcedure.QSP_NguyenNhan_Qty_Crosstab);
                    gridView1.ViewCaption = ReportConstants.ReportName.QSP_NguyenNhan_Qty_Crosstab;
                    break;
                case ReportConstants.ReportId.QSP_NguyenNhan_Amount_Crosstab:
                    gridView1.OptionsBehavior.Editable = false;
                    dt = ReportDAO.ExcuteProcedure(ReportConstants.StoredProcedure.QSP_NguyenNhan_Amount_Crosstab);
                    gridView1.ViewCaption = ReportConstants.ReportName.QSP_NguyenNhan_Amount_Crosstab;
                    break;
                case ReportConstants.ReportId.QMachine_NG_SP_Qty_Crosstab:
                    gridView1.OptionsBehavior.Editable = false;
                    dt = ReportDAO.ExcuteProcedure(ReportConstants.StoredProcedure.QMachine_NG_SP_Qty_Crosstab);
                    gridView1.ViewCaption = ReportConstants.ReportName.QMachine_NG_SP_Qty_Crosstab;
                    break;
                case ReportConstants.ReportId.QMachine_NG_SP_Amount_Crosstab:
                    gridView1.OptionsBehavior.Editable = false;
                    dt = ReportDAO.ExcuteProcedure(ReportConstants.StoredProcedure.QMachine_NG_SP_Amount_Crosstab);
                    gridView1.ViewCaption = ReportConstants.ReportName.QMachine_NG_SP_Amount_Crosstab;
                    break;
                //case ReportConstants.ReportId.FMachine_MonthlyReport:
                //    dt = ReportDAO.ExcuteProcedure(ReportConstants.StoredProcedure.FMachine_MonthlyReport);
                //    gridView1.ViewCaption = ReportConstants.ReportName.FMachine_MonthlyReport;
                //    break;
                case ReportConstants.ReportId.QTonglenhNhapKho1_Crosstab:
                    gridView1.OptionsBehavior.Editable = false;
                    dt = ReportDAO.ExcuteProcedure(ReportConstants.StoredProcedure.QTonglenhNhapKho1_Crosstab);
                    gridView1.ViewCaption = ReportConstants.ReportName.QTonglenhNhapKho1_Crosstab;
                    break;
                case ReportConstants.ReportId.QTonglenhNG_Query_Crosstab:
                    gridView1.OptionsBehavior.Editable = false;
                    dt = ReportDAO.ExcuteProcedure(ReportConstants.StoredProcedure.QTonglenhNG_Query_Crosstab);
                    gridView1.ViewCaption = ReportConstants.ReportName.QTonglenhNG_Query_Crosstab;
                    break;
                //case ReportConstants.ReportId.FNGCu_Moi:
                //    frmDateInput fr = new frmDateInput(this);
                //    fr.StartPosition = FormStartPosition.CenterParent;
                //    fr.ShowDialog();
                //    break;
                case ReportConstants.ReportId.QTonglenhNG_Nguyenlieu_Crosstab:
                    gridView1.OptionsBehavior.Editable = false;
                    dt = ReportDAO.ExcuteProcedure(ReportConstants.StoredProcedure.QTonglenhNG_Nguyenlieu_Crosstab);
                    gridView1.ViewCaption = ReportConstants.ReportName.QTonglenhNG_Nguyenlieu_Crosstab;
                    break;
                case ReportConstants.ReportId.QSSvoitonglenhgiacongOK_SPQty:
                    gridView1.OptionsBehavior.Editable = false;
                    dt = ReportDAO.ExcuteProcedure(ReportConstants.StoredProcedure.QSSvoitonglenhgiacongOK_SPQty);
                    gridView1.ViewCaption = ReportConstants.ReportName.QSSvoitonglenhgiacongOK_SPQty;
                    break;
                case ReportConstants.ReportId.QSSvoitonglenhgiacongOK_SP_USD:
                    gridView1.OptionsBehavior.Editable = false;
                    dt = ReportDAO.ExcuteProcedure(ReportConstants.StoredProcedure.QSSvoitonglenhgiacongOK_SP_USD);
                    gridView1.ViewCaption = ReportConstants.ReportName.QSSvoitonglenhgiacongOK_SP_USD;
                    break;
                case ReportConstants.ReportId.QSSvoitonglenhgiacongNG_Qty:
                    gridView1.OptionsBehavior.Editable = false;
                    dt = ReportDAO.ExcuteProcedure(ReportConstants.StoredProcedure.QSSvoitonglenhgiacongNG_Qty);
                    gridView1.ViewCaption = ReportConstants.ReportName.QSSvoitonglenhgiacongNG_Qty;
                    break;
                case ReportConstants.ReportId.QSSvoitonglenhgiacongNG_USD:
                    gridView1.OptionsBehavior.Editable = false;
                    dt = ReportDAO.ExcuteProcedure(ReportConstants.StoredProcedure.QSSvoitonglenhgiacongNG_USD);
                    gridView1.ViewCaption = ReportConstants.ReportName.QSSvoitonglenhgiacongNG_USD;
                    break;
                case ReportConstants.ReportId.QSSvoitonglenhgiacongSP_Qty:
                    gridView1.OptionsBehavior.Editable = false;
                    dt = ReportDAO.ExcuteProcedure(ReportConstants.StoredProcedure.QSSvoitonglenhgiacongSP_Qty);
                    gridView1.ViewCaption = ReportConstants.ReportName.QSSvoitonglenhgiacongSP_Qty;
                    break;
                case ReportConstants.ReportId.QSSvoitonglenhgiacongSP_USD:
                    gridView1.OptionsBehavior.Editable = false;
                    dt = ReportDAO.ExcuteProcedure(ReportConstants.StoredProcedure.QSSvoitonglenhgiacongSP_USD);
                    gridView1.ViewCaption = ReportConstants.ReportName.QSSvoitonglenhgiacongSP_USD;
                    break;
                case ReportConstants.ReportId.QSSvoitonglenhgiacongOK_SP_Qty_KH_Crosstab:
                    gridView1.OptionsBehavior.Editable = false;
                    dt = ReportDAO.ExcuteProcedure(ReportConstants.StoredProcedure.QSSvoitonglenhgiacongOK_SP_Qty_KH_Crosstab);
                    gridView1.ViewCaption = ReportConstants.ReportName.QSSvoitonglenhgiacongOK_SP_Qty_KH_Crosstab;
                    break;
                case ReportConstants.ReportId.QSSvoitonglenhgiacongOK__USD_KH_Crosstab:
                    gridView1.OptionsBehavior.Editable = false;
                    dt = ReportDAO.ExcuteProcedure(ReportConstants.StoredProcedure.QSSvoitonglenhgiacongOK__USD_KH_Crosstab);
                    gridView1.ViewCaption = ReportConstants.ReportName.QSSvoitonglenhgiacongOK__USD_KH_Crosstab;
                    break;
                case ReportConstants.ReportId.QSSvoitonglenhgiacongNG_Qty_KH_Crosstab:
                    gridView1.OptionsBehavior.Editable = false;
                    dt = ReportDAO.ExcuteProcedure(ReportConstants.StoredProcedure.QSSvoitonglenhgiacongNG_Qty_KH_Crosstab);
                    gridView1.ViewCaption = ReportConstants.ReportName.QSSvoitonglenhgiacongNG_Qty_KH_Crosstab;
                    break;
                case ReportConstants.ReportId.QSSvoitonglenhgiacongNG_USD_KH_Crosstab:
                    gridView1.OptionsBehavior.Editable = false;
                    dt = ReportDAO.ExcuteProcedure(ReportConstants.StoredProcedure.QSSvoitonglenhgiacongNG_USD_KH_Crosstab);
                    gridView1.ViewCaption = ReportConstants.ReportName.QSSvoitonglenhgiacongNG_USD_KH_Crosstab;
                    break;
                case ReportConstants.ReportId.QSSvoitonglenhgiacongSP_Qty_KH_Crosstab:
                    gridView1.OptionsBehavior.Editable = false;
                    dt = ReportDAO.ExcuteProcedure(ReportConstants.StoredProcedure.QSSvoitonglenhgiacongSP_Qty_KH_Crosstab);
                    gridView1.ViewCaption = ReportConstants.ReportName.QSSvoitonglenhgiacongSP_Qty_KH_Crosstab;
                    break;
                case ReportConstants.ReportId.QSSvoitonglenhgiacongSP_USD_KH_Crosstab:
                    gridView1.OptionsBehavior.Editable = false;
                    dt = ReportDAO.ExcuteProcedure(ReportConstants.StoredProcedure.QSSvoitonglenhgiacongSP_USD_KH_Crosstab);
                    gridView1.ViewCaption = ReportConstants.ReportName.QSSvoitonglenhgiacongSP_USD_KH_Crosstab;
                    break;

                    //data
                case ReportConstants.ReportId.Thuy0480:
                    gridView1.OptionsBehavior.Editable = true;
                    GridColumn colIdThuy0480 = new GridColumn()
                        {
                            Caption = String.Format("ID"),
                            FieldName = "ID",
                        };
                    gridView1.Columns.Add(colIdThuy0480);
                    colIdThuy0480.VisibleIndex = -1;

                    GridColumn colDate = new GridColumn()
                        {
                            Caption = String.Format("Date"),
                            FieldName = "Date",
                        };
                    gridView1.Columns.Add(colDate);
                    colDate.VisibleIndex = 0;

                    GridColumn colMonth = new GridColumn()
                        {
                            Caption = String.Format("Month"),
                            FieldName = "Month",
                        };
                    gridView1.Columns.Add(colMonth);
                    colMonth.VisibleIndex = 1;

                    GridColumn colPartNo = new GridColumn()
                        {
                            Caption = String.Format("PartNo"),
                            FieldName = "PartNo",
                        };
                    gridView1.Columns.Add(colPartNo);
                    colPartNo.VisibleIndex = 2;

                    GridColumn colPartName = new GridColumn()
                        {
                            Caption = String.Format("PartName"),
                            FieldName = "PartName",
                        };
                    gridView1.Columns.Add(colPartName);
                    colPartName.VisibleIndex = 3;

                    GridColumn colBusOrder = new GridColumn()
                        {
                            Caption = String.Format("BusOrder"),
                            FieldName = "BusOrder",
                        };
                    gridView1.Columns.Add(colBusOrder);
                    colBusOrder.VisibleIndex = 4;

                    GridColumn colQuantity = new GridColumn()
                        {
                            Caption = String.Format("Quantity"),
                            FieldName = "Quantity",
                        };
                    gridView1.Columns.Add(colQuantity);
                    colQuantity.VisibleIndex = 5;


                    GridColumn colOrder = new GridColumn()
                        {
                            Caption = String.Format("Order"),
                            FieldName = "Order",
                        };
                    gridView1.Columns.Add(colOrder);
                    colOrder.VisibleIndex = 6;


                    GridColumn colF2Order = new GridColumn()
                        {
                            Caption = String.Format("F2Order"),
                            FieldName = "F2Order",
                        };
                    gridView1.Columns.Add(colF2Order);
                    colF2Order.VisibleIndex = 7;


                    GridColumn colOK = new GridColumn()
                        {
                            Caption = String.Format("OK"),
                            FieldName = "OK",
                        };
                    gridView1.Columns.Add(colOK);
                    colOK.VisibleIndex = 8;


                    GridColumn colNG = new GridColumn()
                        {
                            Caption = String.Format("NG"),
                            FieldName = "NG",
                        };
                    gridView1.Columns.Add(colNG);
                    colNG.VisibleIndex = 9;


                    GridColumn colUnitPrice = new GridColumn()
                        {
                            Caption = String.Format("UnitPrice"),
                            FieldName = "UnitPrice",
                        };
                    gridView1.Columns.Add(colUnitPrice);
                    colUnitPrice.VisibleIndex = 10;


                    GridColumn colOKUSD = new GridColumn()
                        {
                            Caption = String.Format("OK_USD"),
                            FieldName = "OK_USD",
                        };
                    gridView1.Columns.Add(colOKUSD);
                    colOKUSD.VisibleIndex = 11;


                    GridColumn colNGUSD = new GridColumn()
                        {
                            Caption = String.Format("NG_USD"),
                            FieldName = "NG_USD",
                        };
                    gridView1.Columns.Add(colNGUSD);
                    colNGUSD.VisibleIndex = 12;


                    GridColumn colCustomer = new GridColumn()
                        {
                            Caption = String.Format("Customer"),
                            FieldName = "Customer",
                        };
                    gridView1.Columns.Add(colCustomer);
                    colCustomer.VisibleIndex = 13;


                    GridColumn colMaterial = new GridColumn()
                        {
                            Caption = String.Format("Material"),
                            FieldName = "Material",
                        };
                    gridView1.Columns.Add(colMaterial);
                    colMaterial.VisibleIndex = 14;


                    GridColumn colMonth1 = new GridColumn()
                        {
                            Caption = String.Format("Month1"),
                            FieldName = "Month1",
                        };
                    gridView1.Columns.Add(colMonth1);
                    colMonth1.VisibleIndex = 15;


                    GridColumn colOK1 = new GridColumn()
                        {
                            Caption = String.Format("OK1"),
                            FieldName = "OK1",
                        };
                    gridView1.Columns.Add(colOK1);
                    colOK1.VisibleIndex = 16;


                    GridColumn colNG1 = new GridColumn()
                        {
                            Caption = String.Format("NG1"),
                            FieldName = "NG1",
                        };
                    gridView1.Columns.Add(colNG1);
                    colNG1.VisibleIndex = 17;


                    GridColumn colEnding = new GridColumn()
                        {
                            Caption = String.Format("Ending"),
                            FieldName = "Ending",
                        };
                    gridView1.Columns.Add(colEnding);
                    colEnding.VisibleIndex = 18;


                    GridColumn colPriceDay = new GridColumn()
                        {
                            Caption = String.Format("PriceDay"),
                            FieldName = "PriceDay",
                        };
                    gridView1.Columns.Add(colPriceDay);
                    colPriceDay.VisibleIndex = 19;


                    GridColumn colY = new GridColumn()
                        {
                            Caption = String.Format("Y"),
                            FieldName = "Y",
                        };
                    gridView1.Columns.Add(colY);
                    colY.VisibleIndex = 20;

                    dt = ReportDAO.ExecuteQuery("SELECT * FROM [242_Thuy0480]");
                    gridView1.ViewCaption = ReportConstants.ReportName.Thuy0480;
                    break;

                case ReportConstants.ReportId.InternalNGDetails:
                    gridView1.OptionsBehavior.Editable = true;
                    GridColumn colIdThuyInternalNGDetails = new GridColumn()
                        {
                            Caption = String.Format("ID"),
                            FieldName = "ID",
                        };
                    gridView1.Columns.Add(colIdThuyInternalNGDetails);
                    colIdThuyInternalNGDetails.VisibleIndex = -1;

                    GridColumn colDateI = new GridColumn()
                        {
                            Caption = String.Format("Date"),
                            FieldName = "Date",
                        };
                    gridView1.Columns.Add(colDateI);
                    colDateI.VisibleIndex = 0;

                    GridColumn colOrderNo = new GridColumn()
                        {
                            Caption = String.Format("OrderNo"),
                            FieldName = "OrderNo",
                        };
                    gridView1.Columns.Add(colOrderNo);
                    colOrderNo.VisibleIndex = 1;

                    GridColumn colQuantityi = new GridColumn()
                        {
                            Caption = String.Format("Quantity"),
                            FieldName = "Quantity",
                        };
                    gridView1.Columns.Add(colQuantityi);
                    colQuantityi.VisibleIndex = 2;

                    GridColumn colStage = new GridColumn()
                        {
                            Caption = String.Format("Stage"),
                            FieldName = "Stage",
                        };
                    gridView1.Columns.Add(colStage);
                    colStage.VisibleIndex = 3;

                    GridColumn colMachineID = new GridColumn()
                        {
                            Caption = String.Format("MachineID"),
                            FieldName = "MachineID",
                        };
                    gridView1.Columns.Add(colMachineID);
                    colMachineID.VisibleIndex = 4;

                    GridColumn colMistakeContent = new GridColumn()
                        {
                            Caption = String.Format("MistakeContent"),
                            FieldName = "MistakeContent",
                        };
                    gridView1.Columns.Add(colMistakeContent);
                    colMistakeContent.VisibleIndex = 5;


                    GridColumn colMistakeID = new GridColumn()
                        {
                            Caption = String.Format("MistakeID"),
                            FieldName = "MistakeID",
                        };
                    gridView1.Columns.Add(colMistakeID);
                    colMistakeID.VisibleIndex = 6;


                    GridColumn colCauseContent = new GridColumn()
                        {
                            Caption = String.Format("CauseContent"),
                            FieldName = "CauseContent",
                        };
                    gridView1.Columns.Add(colCauseContent);
                    colCauseContent.VisibleIndex = 7;


                    GridColumn colCauseID = new GridColumn()
                        {
                            Caption = String.Format("CauseID"),
                            FieldName = "CauseID",
                        };
                    gridView1.Columns.Add(colCauseID);
                    colCauseID.VisibleIndex = 8;


                    GridColumn colKindID = new GridColumn()
                        {
                            Caption = String.Format("KindID"),
                            FieldName = "KindID",
                        };
                    gridView1.Columns.Add(colKindID);
                    colKindID.VisibleIndex = 9;


                    GridColumn colStaffID = new GridColumn()
                        {
                            Caption = String.Format("StaffID"),
                            FieldName = "StaffID",
                        };
                    gridView1.Columns.Add(colStaffID);
                    colStaffID.VisibleIndex = 10;


                    GridColumn colNote = new GridColumn()
                        {
                            Caption = String.Format("Note"),
                            FieldName = "Note",
                        };
                    gridView1.Columns.Add(colNote);
                    colNote.VisibleIndex = 11;

                    dt = ReportDAO.ExecuteQuery("SELECT * FROM [242_InternalNGDetails]");
                    gridView1.ViewCaption = ReportConstants.ReportName.InternalNGDetails;
                    
                    break;

                case ReportConstants.ReportId.MachineList:
                    gridView1.OptionsBehavior.Editable = true;
                    GridColumn colIdMachine = new GridColumn()
                        {
                            Caption = String.Format("ID"),
                            FieldName = "ID",
                        };
                    gridView1.Columns.Add(colIdMachine);
                    colIdMachine.VisibleIndex = -1;

                    GridColumn colMachineIDm = new GridColumn()
                        {
                            Caption = String.Format("MachineID"),
                            FieldName = "MachineID",
                        };
                    gridView1.Columns.Add(colMachineIDm);
                    colMachineIDm.VisibleIndex = 0;

                    GridColumn colMachineGroup1 = new GridColumn()
                        {
                            Caption = String.Format("MachineGroup1"),
                            FieldName = "MachineGroup1",
                        };
                    gridView1.Columns.Add(colMachineGroup1);
                    colMachineGroup1.VisibleIndex = 1;

                    GridColumn colMachineGroup2 = new GridColumn()
                        {
                            Caption = String.Format("MachineGroup2"),
                            FieldName = "MachineGroup2",
                        };
                    gridView1.Columns.Add(colMachineGroup2);
                    colMachineGroup2.VisibleIndex = 2;


                    dt = ReportDAO.ExecuteQuery("SELECT * FROM [242_MachineList]");
                    gridView1.ViewCaption = ReportConstants.ReportName.MachineList;
                    break;

                default:
                    break;
            }

            gridControl1.DataSource = dt;
            //gridView1.PopulateColumns();
            gridView1.BestFitColumns();
        }

        private void SwitchReport(int reportId)
        {
            switch (reportId)
            {
                case ReportConstants.ReportId.FMachine_MonthlyReport:
                    gridView1.ViewCaption = ReportConstants.ReportName.FMachine_MonthlyReport;
                    this.reportId = ReportConstants.ReportId.FMachine_MonthlyReport;
                    
                    frmDateMonthlyReport frMonthlyReport = new frmDateMonthlyReport(this);
                    frMonthlyReport.StartPosition = FormStartPosition.CenterParent;
                    frMonthlyReport.ShowDialog();
                    break;
                case ReportConstants.ReportId.FNGCu_Moi:
                    gridView1.ViewCaption = ReportConstants.ReportName.FNGCu_Moi;
                    this.reportId = ReportConstants.ReportId.FNGCu_Moi;
                    frmDateInput fr = new frmDateInput(this);
                    fr.StartPosition = FormStartPosition.CenterParent;
                    fr.ShowDialog();
                    break;
                case ReportConstants.ReportId.BusOrderofPreviousMonth:
                    gridView1.ViewCaption = ReportConstants.ReportName.BusOrderofPreviousMonth;
                    this.reportId = ReportConstants.ReportId.BusOrderofPreviousMonth;
                    frmDatePreviousMonth fr2 = new frmDatePreviousMonth(this);
                    fr2.StartPosition = FormStartPosition.CenterParent;
                    fr2.ShowDialog();

                    break;
                case ReportConstants.ReportId.BusOrderofThisMonth:
                    gridView1.ViewCaption = ReportConstants.ReportName.BusOrderofThisMonth;
                    this.reportId = ReportConstants.ReportId.BusOrderofThisMonth;

                    frmDateThisMonthFrom fr3 = new frmDateThisMonthFrom(this);
                    fr3.StartPosition = FormStartPosition.CenterParent;
                    fr3.ShowDialog();

                    break;

                default:
                    break;
            }

        }

        public void BindDataToGridviewInput(int reportId)
        {
            gridView1.Columns.Clear();
            gridView1.OptionsBehavior.Editable = false;
            switch (reportId)
            {
                case ReportConstants.ReportId.FMachine_MonthlyReport:

                    var paramMonthlyReport = new SqlParameter[]
                    {
						BaseDAO.CreateParameter("@Month", SqlDbType.NVarChar, month)
                    };
                    if (isCustomer)
                    {
                        dt = ReportDAO.ExcuteProcedure(ReportConstants.StoredProcedure.QMachine_Customer_NG_SP_Qty1, paramMonthlyReport);
                        gridView1.ViewCaption = ReportConstants.ReportName.QMachine_Customer_NG_SP_Qty1;
                    }
                    else if(isMistake)
                    {
                        dt = ReportDAO.ExcuteProcedure(ReportConstants.StoredProcedure.QMachine_Mistake_NG_SP_Amount2, paramMonthlyReport);
                        gridView1.ViewCaption = ReportConstants.ReportName.QMachine_Mistake_NG_SP_Amount2;
                        
                    }
                    break;

                case ReportConstants.ReportId.FNGCu_Moi:

                    var paramFNGCu_Moi = new SqlParameter[]
                    {
						BaseDAO.CreateParameter("@Date", SqlDbType.DateTime, dateInput)
                    };
                    dt = ReportDAO.ExcuteProcedure(ReportConstants.StoredProcedure.FNGCu_Moi, paramFNGCu_Moi);
                    break;

                case ReportConstants.ReportId.BusOrderofPreviousMonth:

                    var paramPreviousMonth = new SqlParameter[]
                    {
						BaseDAO.CreateParameter("@Date", SqlDbType.DateTime, dateInput)
                    };
                    dt = ReportDAO.ExcuteProcedure(ReportConstants.StoredProcedure.BusOrderofPreviousMonth, paramPreviousMonth);

                    break;

                case ReportConstants.ReportId.BusOrderofThisMonth:

                    var paramThisMonth = new SqlParameter[]
                    {
						BaseDAO.CreateParameter("@From", SqlDbType.DateTime, from),
						BaseDAO.CreateParameter("@To", SqlDbType.DateTime, to)
                    };
                    dt = ReportDAO.ExcuteProcedure(ReportConstants.StoredProcedure.BusOrderofThisMonth, paramThisMonth);
                    break;

                default:
                    break;
            }

            gridControl1.DataSource = dt;
            gridView1.PopulateColumns();
            gridView1.BestFitColumns();

            if (this.reportId == ReportConstants.ReportId.BusOrderofPreviousMonth || this.reportId == ReportConstants.ReportId.BusOrderofThisMonth)
            {
                RepositoryItemTextEdit ri = new RepositoryItemTextEdit();
                DevExpress.XtraGrid.Columns.GridColumn col = gridView1.Columns["Imported"];
                col.ColumnEdit = ri;
                col.OptionsColumn.AllowEdit = false;
                ri.CustomDisplayText += new DevExpress.XtraEditors.Controls.CustomDisplayTextEventHandler(ri_CustomDisplayText);
            }


        }

        void ri_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            e.DisplayText = true.Equals(e.Value) ? "True" : "False";
        }

        //Export to excel button

        protected virtual void InitPopupMenu()
        {
            menu = new DXPopupMenu();
            menu.Items.Add(new DXMenuItem("Export all", new EventHandler(OnItemExportAll), null, null, null, null));
            menu.Items.Add(new DXMenuItem("Export selected row(s)", new EventHandler(OnItemExportSelectedRows), null, null, null, null));

        }

        void OnItemExportAll(object sender, EventArgs e)
        {
            Utilities.ExportToExcel(gridView1, 1, true);
        }
        void OnItemExportSelectedRows(object sender, EventArgs e)
        {
            Utilities.ExportToExcel(gridView1, 0, true);
        }

        //report list

        protected virtual void BindDataReportList()
        {
            cmbReportList.Items.Add(new ReportConstants.Item("Select...", -1));

            cmbReportList.Items.Add(new ReportConstants.Item(ReportConstants.ReportName.ChartTong, ReportConstants.ReportId.ChartTong));
            cmbReportList.Items.Add(new ReportConstants.Item("   "+ReportConstants.ReportName.QChartTong_Qty_Crosstab, ReportConstants.ReportId.QChartTong_Qty_Crosstab));
            cmbReportList.Items.Add(new ReportConstants.Item("   " + ReportConstants.ReportName.QChartTong_Amount_Crosstab, ReportConstants.ReportId.QChartTong_Amount_Crosstab));

            cmbReportList.Items.Add(new ReportConstants.Item(ReportConstants.ReportName.ChartNguyenNhanHongSanPham, ReportConstants.ReportId.ChartNguyenNhanHongSanPham));
            cmbReportList.Items.Add(new ReportConstants.Item("   " + ReportConstants.ReportName.QChartNguyenNhan_Qty_Crosstab, ReportConstants.ReportId.QChartNguyenNhan_Qty_Crosstab));
            cmbReportList.Items.Add(new ReportConstants.Item("   " + ReportConstants.ReportName.QChartNguyenNhan_Amount_Crosstab, ReportConstants.ReportId.QChartNguyenNhan_Amount_Crosstab));

            cmbReportList.Items.Add(new ReportConstants.Item(ReportConstants.ReportName.SPND, ReportConstants.ReportId.SPND));
            cmbReportList.Items.Add(new ReportConstants.Item("   " + ReportConstants.ReportName.QSP_ND_Qty_Crosstab, ReportConstants.ReportId.QSP_ND_Qty_Crosstab));
            cmbReportList.Items.Add(new ReportConstants.Item("   " + ReportConstants.ReportName.QSP_ND_Amount_Crosstab, ReportConstants.ReportId.QSP_ND_Amount_Crosstab));

            cmbReportList.Items.Add(new ReportConstants.Item(ReportConstants.ReportName.SPNguyenNhan, ReportConstants.ReportId.SPNguyenNhan));
            cmbReportList.Items.Add(new ReportConstants.Item("   " + ReportConstants.ReportName.QSP_NguyenNhan_Qty_Crosstab, ReportConstants.ReportId.QSP_NguyenNhan_Qty_Crosstab));
            cmbReportList.Items.Add(new ReportConstants.Item("   " + ReportConstants.ReportName.QSP_NguyenNhan_Amount_Crosstab, ReportConstants.ReportId.QSP_NguyenNhan_Amount_Crosstab));

            cmbReportList.Items.Add(new ReportConstants.Item(ReportConstants.ReportName.NGKhoiMay, ReportConstants.ReportId.NGKhoiMay));
            cmbReportList.Items.Add(new ReportConstants.Item("   " + ReportConstants.ReportName.QMachine_NG_SP_Qty_Crosstab, ReportConstants.ReportId.QMachine_NG_SP_Qty_Crosstab));
            cmbReportList.Items.Add(new ReportConstants.Item("   " + ReportConstants.ReportName.QMachine_NG_SP_Amount_Crosstab, ReportConstants.ReportId.QMachine_NG_SP_Amount_Crosstab));
            cmbReportList.Items.Add(new ReportConstants.Item("   " + ReportConstants.ReportName.FMachine_MonthlyReport, ReportConstants.ReportId.FMachine_MonthlyReport));

            cmbReportList.Items.Add(new ReportConstants.Item(ReportConstants.ReportName.SSLenhNGVaTongLenhSX, ReportConstants.ReportId.SSLenhNGVaTongLenhSX));
            cmbReportList.Items.Add(new ReportConstants.Item("   " + ReportConstants.ReportName.TiLeLenhLoiTrenTongSoLenh, ReportConstants.ReportId.TiLeLenhLoiTrenTongSoLenh));
            cmbReportList.Items.Add(new ReportConstants.Item("      " + ReportConstants.ReportName.QTonglenhNhapKho1_Crosstab, ReportConstants.ReportId.QTonglenhNhapKho1_Crosstab));
            cmbReportList.Items.Add(new ReportConstants.Item("      " + ReportConstants.ReportName.QTonglenhNG_Query_Crosstab, ReportConstants.ReportId.QTonglenhNG_Query_Crosstab));
            cmbReportList.Items.Add(new ReportConstants.Item("   " + ReportConstants.ReportName.FNGCu_Moi, ReportConstants.ReportId.FNGCu_Moi));
            cmbReportList.Items.Add(new ReportConstants.Item("   " + ReportConstants.ReportName.QTonglenhNG_Nguyenlieu_Crosstab, ReportConstants.ReportId.QTonglenhNG_Nguyenlieu_Crosstab));

            cmbReportList.Items.Add(new ReportConstants.Item(ReportConstants.ReportName.SsVoiTongHangGiaCong, ReportConstants.ReportId.SsVoiTongHangGiaCong));
            cmbReportList.Items.Add(new ReportConstants.Item("   " + ReportConstants.ReportName.QSSvoitonglenhgiacongOK_SPQty, ReportConstants.ReportId.QSSvoitonglenhgiacongOK_SPQty));
            cmbReportList.Items.Add(new ReportConstants.Item("   " + ReportConstants.ReportName.QSSvoitonglenhgiacongOK_SP_USD, ReportConstants.ReportId.QSSvoitonglenhgiacongOK_SP_USD));
            cmbReportList.Items.Add(new ReportConstants.Item("   " + ReportConstants.ReportName.QSSvoitonglenhgiacongNG_Qty, ReportConstants.ReportId.QSSvoitonglenhgiacongNG_Qty));
            cmbReportList.Items.Add(new ReportConstants.Item("   " + ReportConstants.ReportName.QSSvoitonglenhgiacongNG_USD, ReportConstants.ReportId.QSSvoitonglenhgiacongNG_USD));
            cmbReportList.Items.Add(new ReportConstants.Item("   " + ReportConstants.ReportName.QSSvoitonglenhgiacongSP_Qty, ReportConstants.ReportId.QSSvoitonglenhgiacongSP_Qty));
            cmbReportList.Items.Add(new ReportConstants.Item("   " + ReportConstants.ReportName.QSSvoitonglenhgiacongSP_USD, ReportConstants.ReportId.QSSvoitonglenhgiacongSP_USD));

            cmbReportList.Items.Add(new ReportConstants.Item(ReportConstants.ReportName.NGTheoCacDoiTuongKH, ReportConstants.ReportId.NGTheoCacDoiTuongKH));
            cmbReportList.Items.Add(new ReportConstants.Item("   " + ReportConstants.ReportName.QSSvoitonglenhgiacongOK_SP_Qty_KH_Crosstab, ReportConstants.ReportId.QSSvoitonglenhgiacongOK_SP_Qty_KH_Crosstab));
            cmbReportList.Items.Add(new ReportConstants.Item("   " + ReportConstants.ReportName.QSSvoitonglenhgiacongOK__USD_KH_Crosstab, ReportConstants.ReportId.QSSvoitonglenhgiacongOK__USD_KH_Crosstab));
            cmbReportList.Items.Add(new ReportConstants.Item("   " + ReportConstants.ReportName.QSSvoitonglenhgiacongNG_Qty_KH_Crosstab, ReportConstants.ReportId.QSSvoitonglenhgiacongNG_Qty_KH_Crosstab));
            cmbReportList.Items.Add(new ReportConstants.Item("   " + ReportConstants.ReportName.QSSvoitonglenhgiacongNG_USD_KH_Crosstab, ReportConstants.ReportId.QSSvoitonglenhgiacongNG_USD_KH_Crosstab));
            cmbReportList.Items.Add(new ReportConstants.Item("   " + ReportConstants.ReportName.QSSvoitonglenhgiacongSP_Qty_KH_Crosstab, ReportConstants.ReportId.QSSvoitonglenhgiacongSP_Qty_KH_Crosstab));
            cmbReportList.Items.Add(new ReportConstants.Item("   " + ReportConstants.ReportName.QSSvoitonglenhgiacongSP_USD_KH_Crosstab, ReportConstants.ReportId.QSSvoitonglenhgiacongSP_USD_KH_Crosstab));

            cmbReportList.Items.Add(new ReportConstants.Item(ReportConstants.ReportName.LenhMoi, ReportConstants.ReportId.LenhMoi));
            cmbReportList.Items.Add(new ReportConstants.Item("   " + ReportConstants.ReportName.BusOrderofPreviousMonth, ReportConstants.ReportId.BusOrderofPreviousMonth));
            cmbReportList.Items.Add(new ReportConstants.Item("   " + ReportConstants.ReportName.BusOrderofThisMonth, ReportConstants.ReportId.BusOrderofThisMonth));

            cmbReportList.SelectedItem = cmbReportList.Items[0];

            cmbReportList.DropDown += BCThoigianlamviecthang.ReportConstants.AdjustWidthComboBox;
        }

        private void BindDataItemList()
        {
            cmbData.Items.Add(new ReportConstants.Item("Select..."));
            cmbData.Items.Add(new ReportConstants.Item(ReportConstants.Table.Thuy0480, ReportConstants.ReportId.Thuy0480));
            cmbData.Items.Add(new ReportConstants.Item(ReportConstants.Table.InternalNGDetails, ReportConstants.ReportId.InternalNGDetails));
            cmbData.Items.Add(new ReportConstants.Item(ReportConstants.Table.MachineList, ReportConstants.ReportId.MachineList));

            cmbData.SelectedItem = cmbData.Items[0];

        }

        private void btImport_Click(object sender, EventArgs e)
        {
            FileDialog fileDialog = new OpenFileDialog();
            fileDialog.Filter = "Excel 2007-2013|*.xlsx|Excel 97-2003 (*.xls)|*.xls";
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                OleDbConnection connection = new OleDbConnection();
                try
                {
                    string path = fileDialog.FileName;
                    INISettings iniSettings = new INISettings();
                    string connectString = iniSettings.SQLConnectionString;
                    // Connection String to Excel Workbook
                    string excelConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 12.0;Persist Security Info=False", path);

                    connection.ConnectionString = excelConnectionString;
                    OleDbCommand command = new OleDbCommand("select * from [Sheet1$]", connection);
                    connection.Open();
                    // Create DbDataReader to Data Worksheet
                    DbDataReader dr = command.ExecuteReader();

                    // Bulk Copy to SQL Server 
                    SqlBulkCopy bulkInsert = new SqlBulkCopy(connectString);
                    switch (reportId)
                    {
                        case ReportConstants.ReportId.Thuy0480:
                            SqlBulkCopyColumnMapping date = new SqlBulkCopyColumnMapping("Date", "Date");
                            bulkInsert.ColumnMappings.Add(date);

                            SqlBulkCopyColumnMapping month = new SqlBulkCopyColumnMapping("Month", "Month");
                            bulkInsert.ColumnMappings.Add(month);

                            SqlBulkCopyColumnMapping cPartNo = new SqlBulkCopyColumnMapping("PartNo", "PartNo");
                            bulkInsert.ColumnMappings.Add(cPartNo);

                            SqlBulkCopyColumnMapping cPartName = new SqlBulkCopyColumnMapping("PartName", "PartName");
                            bulkInsert.ColumnMappings.Add(cPartName);

                            SqlBulkCopyColumnMapping nBusOrder = new SqlBulkCopyColumnMapping("BusOrder", "BusOrder");
                            bulkInsert.ColumnMappings.Add(nBusOrder);

                            SqlBulkCopyColumnMapping aQuantity = new SqlBulkCopyColumnMapping("Quantity", "Quantity");
                            bulkInsert.ColumnMappings.Add(aQuantity);

                            SqlBulkCopyColumnMapping aOrder = new SqlBulkCopyColumnMapping("Order", "Order");
                            bulkInsert.ColumnMappings.Add(aOrder);

                            SqlBulkCopyColumnMapping cF2Order = new SqlBulkCopyColumnMapping("F2Order", "F2Order");
                            bulkInsert.ColumnMappings.Add(cF2Order);

                            SqlBulkCopyColumnMapping cOK = new SqlBulkCopyColumnMapping("OK", "OK");
                            bulkInsert.ColumnMappings.Add(cOK);

                            SqlBulkCopyColumnMapping cNG = new SqlBulkCopyColumnMapping("NG", "NG");
                            bulkInsert.ColumnMappings.Add(cNG);

                            SqlBulkCopyColumnMapping cUnitPrice = new SqlBulkCopyColumnMapping("UnitPrice", "UnitPrice");
                            bulkInsert.ColumnMappings.Add(cUnitPrice);

                            SqlBulkCopyColumnMapping cOKUSD = new SqlBulkCopyColumnMapping("OK_USD", "OK_USD");
                            bulkInsert.ColumnMappings.Add(cOKUSD);

                            SqlBulkCopyColumnMapping cNG_USD = new SqlBulkCopyColumnMapping("NG_USD", "NG_USD");
                            bulkInsert.ColumnMappings.Add(cNG_USD);

                            SqlBulkCopyColumnMapping cCustomer = new SqlBulkCopyColumnMapping("Customer", "Customer");
                            bulkInsert.ColumnMappings.Add(cCustomer);

                            SqlBulkCopyColumnMapping cMaterial = new SqlBulkCopyColumnMapping("Material", "Material");
                            bulkInsert.ColumnMappings.Add(cMaterial);

                            SqlBulkCopyColumnMapping cMonth1 = new SqlBulkCopyColumnMapping("Month1", "Month1");
                            bulkInsert.ColumnMappings.Add(cMonth1);

                            SqlBulkCopyColumnMapping cOK1 = new SqlBulkCopyColumnMapping("OK1", "OK1");
                            bulkInsert.ColumnMappings.Add(cOK1);

                            SqlBulkCopyColumnMapping cNG1 = new SqlBulkCopyColumnMapping("NG1", "NG1");
                            bulkInsert.ColumnMappings.Add(cNG1);

                            SqlBulkCopyColumnMapping cEnding = new SqlBulkCopyColumnMapping("Ending", "Ending");
                            bulkInsert.ColumnMappings.Add(cEnding);

                            SqlBulkCopyColumnMapping cPriceDay = new SqlBulkCopyColumnMapping("PriceDay", "PriceDay");
                            bulkInsert.ColumnMappings.Add(cPriceDay);

                            SqlBulkCopyColumnMapping cY = new SqlBulkCopyColumnMapping("Y", "Y");
                            bulkInsert.ColumnMappings.Add(cY);

                            bulkInsert.DestinationTableName = "[rrc_database].[dbo].[242_Thuy0480]";
                            bulkInsert.WriteToServer(dr);

                            break;

                        case ReportConstants.ReportId.InternalNGDetails:
                            SqlBulkCopyColumnMapping cDate = new SqlBulkCopyColumnMapping("Date", "Date");
                            bulkInsert.ColumnMappings.Add(cDate);

                            SqlBulkCopyColumnMapping cOrderNoe = new SqlBulkCopyColumnMapping("OrderNo", "OrderNo");
                            bulkInsert.ColumnMappings.Add(cOrderNoe);

                            SqlBulkCopyColumnMapping dQuantity = new SqlBulkCopyColumnMapping("Quantity", "Quantity");
                            bulkInsert.ColumnMappings.Add(dQuantity);

                            SqlBulkCopyColumnMapping gStage = new SqlBulkCopyColumnMapping("Stage", "Stage");
                            bulkInsert.ColumnMappings.Add(gStage);

                            SqlBulkCopyColumnMapping gMachineID = new SqlBulkCopyColumnMapping("MachineID", "MachineID");
                            bulkInsert.ColumnMappings.Add(gMachineID);

                            SqlBulkCopyColumnMapping dMistakeContent = new SqlBulkCopyColumnMapping("MistakeContent", "MistakeContent");
                            bulkInsert.ColumnMappings.Add(dMistakeContent);

                            SqlBulkCopyColumnMapping dMistakeID = new SqlBulkCopyColumnMapping("MistakeID", "MistakeID");
                            bulkInsert.ColumnMappings.Add(dMistakeID);

                            SqlBulkCopyColumnMapping dCauseContent = new SqlBulkCopyColumnMapping("CauseContent", "CauseContent");
                            bulkInsert.ColumnMappings.Add(dCauseContent);

                            SqlBulkCopyColumnMapping dCauseID = new SqlBulkCopyColumnMapping("CauseID", "CauseID");
                            bulkInsert.ColumnMappings.Add(dCauseID);

                            SqlBulkCopyColumnMapping dKindID = new SqlBulkCopyColumnMapping("KindID", "KindID");
                            bulkInsert.ColumnMappings.Add(dKindID);

                            SqlBulkCopyColumnMapping dStaffID = new SqlBulkCopyColumnMapping("StaffID", "StaffID");
                            bulkInsert.ColumnMappings.Add(dStaffID);

                            SqlBulkCopyColumnMapping dNote = new SqlBulkCopyColumnMapping("Note", "Note");
                            bulkInsert.ColumnMappings.Add(dNote);

                            bulkInsert.DestinationTableName = "[rrc_database].[dbo].[242_InternalNGDetails]";
                            bulkInsert.WriteToServer(dr);

                            break;

                        case ReportConstants.ReportId.MachineList:
                            SqlBulkCopyColumnMapping cMachineID = new SqlBulkCopyColumnMapping("MachineID", "MachineID");
                            bulkInsert.ColumnMappings.Add(cMachineID);

                            SqlBulkCopyColumnMapping sMachineGroup1 = new SqlBulkCopyColumnMapping("MachineGroup1", "MachineGroup1");
                            bulkInsert.ColumnMappings.Add(sMachineGroup1);

                            SqlBulkCopyColumnMapping dMachineGroup2 = new SqlBulkCopyColumnMapping("MachineGroup2", "MachineGroup2");
                            bulkInsert.ColumnMappings.Add(dMachineGroup2);

                            bulkInsert.DestinationTableName = "[rrc_database].[dbo].[242_MachineList]";
                            bulkInsert.WriteToServer(dr);

                            break;
                    }



                    connection.Close();
                    MessageBox.Show("Import data successfully");
                    BindDataToGridview(reportId);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    connection.Close();
                }

            }
        }

        private void btDelete_Click(object sender, EventArgs e)
        {
            if (gridView1.SelectedRowsCount > 0)
            {
                var row = new DataRow[gridView1.SelectedRowsCount];
                var workNameId = new string[gridView1.SelectedRowsCount];
                string delete = "";
                switch (reportId)
                {
                    case ReportConstants.ReportId.Thuy0480:
                        delete = "delete from [242_Thuy0480] where ID=";
                        break;
                    case ReportConstants.ReportId.InternalNGDetails:
                        delete = "delete from [242_InternalNGDetails] where ID=";
                        break;
                    case ReportConstants.ReportId.MachineList:
                        delete = "delete from [242_MachineList] where ID=";
                        break;
                    default:
                        return;
                        break;
                }


                for (int i = 0; i < gridView1.SelectedRowsCount; i++)
                {
                    row[i] = gridView1.GetDataRow(gridView1.GetSelectedRows()[i]);
                    workNameId[i] = row[i]["ID"].ToString();
                }


                if (MessageBox.Show(ReportConstants.Message.DeleteQuestion, "Warning",
                                MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    for (int i = 0; i < workNameId.Length; i++)
                    {
                        string sql = delete + workNameId[i];
                        if (ReportDAO.ExecuteNonQuery(sql) > 0)
                        {
                            // _action = 2;
                        }
                        else
                        {
                            MessageBox.Show(ReportConstants.Message.DeleteFailed, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }

                    BindDataToGridview(reportId);

                }
            }
            else
            {
                MessageBox.Show(ToolConstants.Message.TableIsNull, ToolConstants.Form.MessageBox.Message, MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
            }
        }

        private void gridView1_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            GridView view = sender as GridView;
            switch (reportId)
            {
                case ReportConstants.ReportId.Thuy0480:

                    //Kiểm tra đây là dòng dữ liệu mới hay cũ, nếu là mới thì mình insert
                    if (view.IsNewItemRow(e.RowHandle))
                    {
                        //e.RowHandle trả về giá trị int là thứ tự của dòng hiện tại

                    }
                    //Cũ thì update
                    else
                    {
                        string date = (view.GetRowCellValue(e.RowHandle, view.Columns["Date"]).ToString() == "") ? "null" : view.GetRowCellValue(e.RowHandle, view.Columns["Date"]).ToString();
                        string month = (view.GetRowCellValue(e.RowHandle, view.Columns["Month"]).ToString() == "") ? "null" : view.GetRowCellValue(e.RowHandle, view.Columns["Month"]).ToString();
                        string partNo = (view.GetRowCellValue(e.RowHandle, view.Columns["PartNo"]).ToString() == "") ? "null" : "N'" + view.GetRowCellValue(e.RowHandle, view.Columns["PartNo"]).ToString() + "'";
                        string partName = (view.GetRowCellValue(e.RowHandle, view.Columns["PartName"]).ToString() == "") ? "null" : "N'" + view.GetRowCellValue(e.RowHandle, view.Columns["PartName"]).ToString() + "'";
                        string busOrder = (view.GetRowCellValue(e.RowHandle, view.Columns["BusOrder"]).ToString() == "") ? "null" : "N'" + view.GetRowCellValue(e.RowHandle, view.Columns["BusOrder"]).ToString() + "'";
                        string quantity = (view.GetRowCellValue(e.RowHandle, view.Columns["Quantity"]).ToString() == "") ? "null" : view.GetRowCellValue(e.RowHandle, view.Columns["Quantity"]).ToString();
                        string order = (view.GetRowCellValue(e.RowHandle, view.Columns["Order"]).ToString() == "") ? "null" : "N'" + view.GetRowCellValue(e.RowHandle, view.Columns["Order"]).ToString() + "'";
                        string f2Order = (view.GetRowCellValue(e.RowHandle, view.Columns["F2Order"]).ToString() == "") ? "null" : "N'" + view.GetRowCellValue(e.RowHandle, view.Columns["F2Order"]).ToString() + "'";
                        string oK = (view.GetRowCellValue(e.RowHandle, view.Columns["OK"]).ToString() == "") ? "null" : view.GetRowCellValue(e.RowHandle, view.Columns["OK"]).ToString();
                        string nG = (view.GetRowCellValue(e.RowHandle, view.Columns["NG"]).ToString() == "") ? "null" : view.GetRowCellValue(e.RowHandle, view.Columns["NG"]).ToString();
                        string unitPrice = (view.GetRowCellValue(e.RowHandle, view.Columns["UnitPrice"]).ToString() == "") ? "null" : view.GetRowCellValue(e.RowHandle, view.Columns["UnitPrice"]).ToString();
                        string oK_USD = (view.GetRowCellValue(e.RowHandle, view.Columns["OK_USD"]).ToString() == "") ? "null" : view.GetRowCellValue(e.RowHandle, view.Columns["OK_USD"]).ToString();
                        string nG_USD = (view.GetRowCellValue(e.RowHandle, view.Columns["NG_USD"]).ToString() == "") ? "null" : view.GetRowCellValue(e.RowHandle, view.Columns["NG_USD"]).ToString();
                        string customer = (view.GetRowCellValue(e.RowHandle, view.Columns["Customer"]).ToString() == "") ? "null" : "N'" + view.GetRowCellValue(e.RowHandle, view.Columns["Customer"]).ToString() + "'";
                        string material = (view.GetRowCellValue(e.RowHandle, view.Columns["Material"]).ToString() == "") ? "null" : "N'" + view.GetRowCellValue(e.RowHandle, view.Columns["Material"]).ToString() + "'";
                        string month1 = (view.GetRowCellValue(e.RowHandle, view.Columns["Month1"]).ToString() == "") ? "null" : "N'" + view.GetRowCellValue(e.RowHandle, view.Columns["Month1"]).ToString() + "'";
                        string oK1 = (view.GetRowCellValue(e.RowHandle, view.Columns["OK1"]).ToString() == "") ? "null" : "N'" + view.GetRowCellValue(e.RowHandle, view.Columns["OK1"]).ToString() + "'";
                        string nG1 = (view.GetRowCellValue(e.RowHandle, view.Columns["NG1"]).ToString() == "") ? "null" : "N'" + view.GetRowCellValue(e.RowHandle, view.Columns["NG1"]).ToString() + "'";
                        string ending = (view.GetRowCellValue(e.RowHandle, view.Columns["Ending"]).ToString() == "") ? "null" : view.GetRowCellValue(e.RowHandle, view.Columns["Ending"]).ToString();
                        string priceDay = (view.GetRowCellValue(e.RowHandle, view.Columns["PriceDay"]).ToString() == "") ? "null" : view.GetRowCellValue(e.RowHandle, view.Columns["PriceDay"]).ToString();
                        string pY = (view.GetRowCellValue(e.RowHandle, view.Columns["Y"]).ToString() == "") ? "null" : view.GetRowCellValue(e.RowHandle, view.Columns["Y"]).ToString();
                        string id = (view.GetRowCellValue(e.RowHandle, view.Columns["ID"]).ToString() == "") ? "null" : view.GetRowCellValue(e.RowHandle, view.Columns["ID"]).ToString();

                        string update = "update [242_Thuy0480] set [Date]="+ date 
                            + ", Month =" + month 
                            + ", PartNo =" + partNo 
                            + ", [PartName] =" + partName 
                            + ", [BusOrder] ="+ busOrder
                            + ", [Quantity] =" + quantity
                            + ", [Order] =" + order 
                            + ", [F2Order] =" + f2Order
                            + ", [OK] =" + oK
                            + ", [NG] =" + nG
                            + ", [UnitPrice] =" + unitPrice
                            + ", [OK_USD] =" + oK_USD
                            + ", [NG_USD] =" + nG_USD
                            + ", [Customer] =" + customer 
                            + ", [Material] =" + material
                            + ", [Month1] =" + month1
                            + ", [OK1] =" + oK1 
                            + ", [NG1] =" + nG1
                            + ", [Ending] =" + ending
                            + ", [PriceDay] =" + priceDay 
                            + ", [Y] =" + pY
                            + " where [ID] =" + id;
                        ReportDAO.ExecuteNonQuery(update);

                    }
                    break;

                case ReportConstants.ReportId.InternalNGDetails:

                    //Kiểm tra đây là dòng dữ liệu mới hay cũ, nếu là mới thì mình insert
                    if (view.IsNewItemRow(e.RowHandle))
                    {
                        //e.RowHandle trả về giá trị int là thứ tự của dòng hiện tại

                    }
                    //Cũ thì update
                    else
                    {
                        //string date = (view.GetRowCellValue(e.RowHandle, view.Columns["Date"]).ToString() == "") ? "null" : view.GetRowCellValue(e.RowHandle, view.Columns["Date"]).ToString();
                        //string month = (view.GetRowCellValue(e.RowHandle, view.Columns["OrderNo"]).ToString() == "") ? "null" : view.GetRowCellValue(e.RowHandle, view.Columns["Month"]).ToString();
                        //string partNo = (view.GetRowCellValue(e.RowHandle, view.Columns["Quantity"]).ToString() == "") ? "null" : "N'" + view.GetRowCellValue(e.RowHandle, view.Columns["PartNo"]).ToString() + "'";
                        //string partName = (view.GetRowCellValue(e.RowHandle, view.Columns["PartName"]).ToString() == "") ? "null" : "N'" + view.GetRowCellValue(e.RowHandle, view.Columns["PartName"]).ToString() + "'";
                        //string busOrder = (view.GetRowCellValue(e.RowHandle, view.Columns["BusOrder"]).ToString() == "") ? "null" : "N'" + view.GetRowCellValue(e.RowHandle, view.Columns["BusOrder"]).ToString() + "'";
                        //string quantity = (view.GetRowCellValue(e.RowHandle, view.Columns["Quantity"]).ToString() == "") ? "null" : view.GetRowCellValue(e.RowHandle, view.Columns["Quantity"]).ToString();
                        //string order = (view.GetRowCellValue(e.RowHandle, view.Columns["Order"]).ToString() == "") ? "null" : "N'" + view.GetRowCellValue(e.RowHandle, view.Columns["Order"]).ToString() + "'";
                        //string f2Order = (view.GetRowCellValue(e.RowHandle, view.Columns["F2Order"]).ToString() == "") ? "null" : "N'" + view.GetRowCellValue(e.RowHandle, view.Columns["F2Order"]).ToString() + "'";
                        //string oK = (view.GetRowCellValue(e.RowHandle, view.Columns["OK"]).ToString() == "") ? "null" : view.GetRowCellValue(e.RowHandle, view.Columns["OK"]).ToString();
                        //string nG = (view.GetRowCellValue(e.RowHandle, view.Columns["NG"]).ToString() == "") ? "null" : view.GetRowCellValue(e.RowHandle, view.Columns["NG"]).ToString();
                        //string unitPrice = (view.GetRowCellValue(e.RowHandle, view.Columns["UnitPrice"]).ToString() == "") ? "null" : view.GetRowCellValue(e.RowHandle, view.Columns["UnitPrice"]).ToString();
                        //string oK_USD = (view.GetRowCellValue(e.RowHandle, view.Columns["OK_USD"]).ToString() == "") ? "null" : view.GetRowCellValue(e.RowHandle, view.Columns["OK_USD"]).ToString();
                        //string nG_USD = (view.GetRowCellValue(e.RowHandle, view.Columns["NG_USD"]).ToString() == "") ? "null" : view.GetRowCellValue(e.RowHandle, view.Columns["NG_USD"]).ToString();
                        //string customer = (view.GetRowCellValue(e.RowHandle, view.Columns["Customer"]).ToString() == "") ? "null" : "N'" + view.GetRowCellValue(e.RowHandle, view.Columns["Customer"]).ToString() + "'";
                        //string material = (view.GetRowCellValue(e.RowHandle, view.Columns["Material"]).ToString() == "") ? "null" : "N'" + view.GetRowCellValue(e.RowHandle, view.Columns["Material"]).ToString() + "'";
                        //string month1 = (view.GetRowCellValue(e.RowHandle, view.Columns["Month1"]).ToString() == "") ? "null" : "N'" + view.GetRowCellValue(e.RowHandle, view.Columns["Month1"]).ToString() + "'";
                        //string oK1 = (view.GetRowCellValue(e.RowHandle, view.Columns["OK1"]).ToString() == "") ? "null" : "N'" + view.GetRowCellValue(e.RowHandle, view.Columns["OK1"]).ToString() + "'";
                        //string nG1 = (view.GetRowCellValue(e.RowHandle, view.Columns["NG1"]).ToString() == "") ? "null" : "N'" + view.GetRowCellValue(e.RowHandle, view.Columns["NG1"]).ToString() + "'";
                        //string ending = (view.GetRowCellValue(e.RowHandle, view.Columns["Ending"]).ToString() == "") ? "null" : view.GetRowCellValue(e.RowHandle, view.Columns["Ending"]).ToString();
                        //string priceDay = (view.GetRowCellValue(e.RowHandle, view.Columns["PriceDay"]).ToString() == "") ? "null" : view.GetRowCellValue(e.RowHandle, view.Columns["PriceDay"]).ToString();
                        //string pY = (view.GetRowCellValue(e.RowHandle, view.Columns["Y"]).ToString() == "") ? "null" : view.GetRowCellValue(e.RowHandle, view.Columns["Y"]).ToString();
                        //string id = (view.GetRowCellValue(e.RowHandle, view.Columns["ID"]).ToString() == "") ? "null" : view.GetRowCellValue(e.RowHandle, view.Columns["ID"]).ToString();

                        string update = "update [242_InternalNGDetails] set [Date]=N'"
                       + view.GetRowCellValue(e.RowHandle, view.Columns["Date"]).ToString() + "', OrderNo =N'"
                       + view.GetRowCellValue(e.RowHandle, view.Columns["OrderNo"]).ToString() + "', Quantity ="
                       + view.GetRowCellValue(e.RowHandle, view.Columns["Quantity"]).ToString() + ", [Stage] =N'"
                        + view.GetRowCellValue(e.RowHandle, view.Columns["Stage"]).ToString() + "', [MachineID] =N'"
                        + view.GetRowCellValue(e.RowHandle, view.Columns["MachineID"]).ToString() + "', [MistakeContent] =N'"
                        + view.GetRowCellValue(e.RowHandle, view.Columns["MistakeContent"]).ToString() + "', [MistakeID] =N'"
                        + view.GetRowCellValue(e.RowHandle, view.Columns["MistakeID"]).ToString() + "', [CauseContent] =N'"
                        + view.GetRowCellValue(e.RowHandle, view.Columns["CauseContent"]).ToString() + "', [CauseID] =N'"
                        + view.GetRowCellValue(e.RowHandle, view.Columns["CauseID"]).ToString() + "', [KindID] =N'"
                        + view.GetRowCellValue(e.RowHandle, view.Columns["KindID"]).ToString() + "', [StaffID] =N'"
                        + view.GetRowCellValue(e.RowHandle, view.Columns["StaffID"]).ToString() + ", [Note] =N'"
                        + view.GetRowCellValue(e.RowHandle, view.Columns["Note"]).ToString() + "' where [ID] ="
                       + view.GetRowCellValue(e.RowHandle, view.Columns["ID"]).ToString();
                        ReportDAO.ExecuteNonQuery(update);

                    }
                    break;

                case ReportConstants.ReportId.MachineList:

                    //Kiểm tra đây là dòng dữ liệu mới hay cũ, nếu là mới thì mình insert
                    if (view.IsNewItemRow(e.RowHandle))
                    {
                        //e.RowHandle trả về giá trị int là thứ tự của dòng hiện tại

                    }
                    //Cũ thì update
                    else
                    {
                        string update = "update [242_MachineList] set [MachineID]=N'"
                       + view.GetRowCellValue(e.RowHandle, view.Columns["MachineID"]).ToString() + "', MachineGroup1 =N'"
                       + view.GetRowCellValue(e.RowHandle, view.Columns["MachineGroup1"]).ToString() + "', MachineGroup2 =N'"
                       + view.GetRowCellValue(e.RowHandle, view.Columns["MachineGroup2"]).ToString() + "' where [ID] ="
                       + view.GetRowCellValue(e.RowHandle, view.Columns["ID"]).ToString();
                        ReportDAO.ExecuteNonQuery(update);

                    }
                    break;
            }
        }

    }

}
